﻿using System;
using NHibernate.Cfg;
using StructureMap;
using StructureMap.Configuration.DSL;
using NHibernate;
using NHibernate.Criterion;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate.Cache;
using NHibernate.Context;
using NServiceBus.Saga;
using System.Data;

namespace Subscriber1
{
    /*
    SQLiteConfiguration a =
        SQLiteConfiguration
            .Standard
            .ConnectionString("data source=C:\\Source\\Database\\Sagas.db")
            .CurrentSessionContext<ThreadStaticSessionContext>();
            //.IsolationLevel(IsolationLevel.ReadUncommitted)

    Config = Fluently.Configure()
        .Database(a)
        .Mappings(m => m.FluentMappings.AddFromAssemblyOf<MySagaDataMap>()).BuildConfiguration();*/
    public class SagaRegistry : Registry, IDisposable
    {
        public Configuration Config { get; set; }
        public ISessionFactory Factory { get; set; }

        public SagaRegistry()
        {
            Config = Fluently.Configure().Database(
                MsSqlConfiguration.MsSql2005.ConnectionString("Server=(local);Database=AdnanLocal;Trusted_Connection=true;")
                    .Cache(c =>
                           c.UseQueryCache()
                           .QueryCacheFactory<StandardQueryCacheFactory>()
                           .ProviderClass<HashtableCacheProvider>()
                           .UseMinimalPuts())
                    .UseReflectionOptimizer()
                    .MaxFetchDepth(2)
                    .AdoNetBatchSize(24)
                    .CurrentSessionContext<ThreadStaticSessionContext>()
                ).Mappings(m => m.FluentMappings.AddFromAssemblyOf<SagaHeaderDataMap>()).BuildConfiguration();

            Config.SetProperty(NHibernate.Cfg.Environment.GenerateStatistics, "true");
            Config.SetProperty(NHibernate.Cfg.Environment.PropertyUseReflectionOptimizer, "true");

            ForSingletonOf<Configuration>().Use(x => Config);

            Factory = Config.BuildSessionFactory();
            ForSingletonOf<ISessionFactory>()
                .Use(x => Factory)
                .Named("ISessionFactoryInstance");

            For<ISagaPersister>().Use<MySagaPersister>();
        }

        #region IDisposable Members

        public void Dispose()
        {
            
        }

        #endregion
    }
}
